package com.example.justbuy.dome.domeActiviti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class dome2{
    ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();

    /**
     * 改变Assigbss的值
     * */
    @Test
    public void updeAssignee() {
        String assignee = "dd";
        String did = "210001";
        List<Task> tasks = processEngine.getTaskService().createTaskQuery().processInstanceId(did).list();
        for (Task task : tasks) {
            System.out.println("李四的任务：name:" + task.getName() + ",id:" + task.getId());
            processEngine.getTaskService().claim(task.getId(), assignee);
        }
    }

    @Test
    public void getProcessInstanceid(){
        //流程定义的key
        String taskId="12523";
        Task task=processEngine.getTaskService().createTaskQuery() // 创建任务查询
                .taskId(taskId) // 根据任务id查询
                .singleResult();
        System.out.println("流程实例id：" + task.getProcessInstanceId());
        System.out.println("任务id：" + task.getId());
        System.out.println("任务负责人：" + task.getAssignee());
        System.out.println("任务名称：" + task.getName());
    }
    /**
     * 根据类型查找任务
     */
    @Test
    public void updAssignee() {
        //List<Task> tasks =
        List<Task> list = processEngine.getTaskService().createTaskQuery()
                        .taskName("商家审核").list();
        if (list!=null&&list.size()>0){
            for (Task task : list) {
                System.out.println("流程实例id：" + task.getProcessInstanceId());
                System.out.println("任务id：" + task.getId());
                System.out.println("任务负责人：" + task.getAssignee());
                System.out.println("任务名称：" + task.getName());
            }
        }
    }
    @Test
    public void ddd(){
        String taskId="22526";
        String typeName="入驻";
////        String xx=(String) processEngine.getTaskService().getVariable(taskId, "typename");
//        Object obj=processEngine.getTaskService().getVariable(taskId, typeName);
//        System.out.println("1111:"+obj.getClass());
//        HashMap mm=(HashMap)obj;
//        Set allkey= mm.keySet();
//        System.out.println("2222:"+allkey);
//        ObjectInputStream ois=(ObjectInputStream)processEngine.getTaskService().getVariable(taskId, typeName);
//        Object oj= processEngine.getTaskService().getVariable(taskId, typeName);

        Map<String,Object> map=(HashMap<String,Object>)processEngine.getTaskService().getVariables(taskId);
        //Map<String,Object> map=(Map<String,Object>)in.readObject();
        for(Map.Entry<String, Object> entry : map.entrySet()){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
//        System.out.println(xx);
    }

    @Test
    public void completeTask(){
        //任务ID
        String taskId = "12523";
        //完成任务的同时，设置流程变量，让流程变量判断连线该如何执行
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("typename", "入驻");
        processEngine.getTaskService()//
                .complete(taskId,variables);
        System.out.println("完成任务："+taskId);
    }

}
